package com.tian.view.draw.fermode

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode
import android.util.AttributeSet
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.view.View
import com.tian.util.view.DisplayUtil

/**
 * @author tiansongtao
 * @date 2023/1/6 15:29
 */
class DrawEraserView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
        View(context, attrs, defStyleAttr) {

    private val TAG = "tiansongtao"

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null)

    init {
        setLayerType(LAYER_TYPE_SOFTWARE, null)
    }

    private val paint = Paint().apply {
        color = Color.RED
        style = Paint.Style.STROKE
        strokeWidth = 45f
//        pathEffect = CornerPathEffect(20f)
//        strokeCap = Paint.Cap.ROUND
    }

    private val textPaint = Paint().apply {
        color = Color.BLACK
        style = Paint.Style.FILL_AND_STROKE
        textSize = DisplayUtil.spToPx(14f)
        isAntiAlias = true
        textAlign = Paint.Align.CENTER
    }


    private val bitmap: Bitmap by lazy {
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
        val bc = Canvas(bitmap)
        bc.drawColor(Color.GRAY)
        bitmap
    }
    private lateinit var pathBitmap: Bitmap

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), 800)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        pathBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
    }

    private val xFermode = PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)
    private val path = Path()

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawText("橡皮擦功能-SRC_OUT", width / 2f, 50f, textPaint)
        canvas.translate(0f, 70f)
        val layerId = canvas.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null)

        val pathc = Canvas(pathBitmap)
        pathc.drawPath(path, paint)

        canvas.drawBitmap(pathBitmap, 0f, 0f, paint)
        paint.xfermode = xFermode
        canvas.drawBitmap(bitmap, 0f, 0f, paint)
        paint.xfermode = null
        canvas.restoreToCount(layerId)
    }


    private var time = 0L
    private var mPreX = 0f
    private var mPreY = 0f


    val gestureDetector = GestureDetector(context, object : SimpleOnGestureListener() {
        override fun onDown(e: MotionEvent): Boolean {
            time = System.currentTimeMillis()
            path.moveTo(e.x, e.y)
            mPreX = e.x
            mPreY = e.y
            return true
        }

        override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float): Boolean {
            val endX = (mPreX + e2.x) / 2
            val endY = (mPreY + e2.y) / 2
            path.quadTo(mPreX, mPreY, endX, endY)
            mPreX = e2.x
            mPreY = e2.y
            postInvalidate()
            return true
        }

        override fun onSingleTapUp(e: MotionEvent?): Boolean {
            if (System.currentTimeMillis() - time < 500) {
                path.reset()
                pathBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
                postInvalidate()
            }
            return true
        }

    })

    override fun onTouchEvent(e: MotionEvent): Boolean {
        return gestureDetector.onTouchEvent(e)
    }

}